package DARCOServer.db;

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.sql.Date;
import java.util.Vector;



import javax.imageio.ImageIO;

import org.json.JSONException;
import org.json.JSONObject;
import org.tmatesoft.sqljet.core.SqlJetException;
import org.tmatesoft.sqljet.core.table.ISqlJetTable;
import org.tmatesoft.sqljet.core.table.SqlJetDb;

import DARCOServer.security.SecurityManager;
import DARCOShared.utils.SecurityUtilities;
import static DARCOShared.utils.Constants.*;

public class CardDataToServerDB {
	
	//data field
	//categories
	private Vector<Integer> categoryID= new Vector<Integer>();
	private Vector<String> categories= new Vector<String>();
	private Vector<Integer> allowSub= new Vector<Integer>();
	
	//cards
	private Vector<String> cardSN = new Vector<String>();
	private Vector<String> cardName = new Vector<String>();
	private Vector<String> cardStats = new Vector<String>();
	private Vector<String> cardSpecial = new Vector<String>();
	private Vector<Integer> cardCat = new Vector<Integer>();
	private Vector<Integer> cardSubCat = new Vector<Integer>();
	private Vector<byte[]> cardPreview = new Vector<byte[]>();
	private Vector<byte[]> cardImage = new Vector<byte[]>();
	
	//Unique card fields
	private Vector<String> uniqueSN = new Vector<String>();
	private Vector<String> uniqueID = new Vector<String>();
	private Vector<String> uniqueActOwn = new Vector<String>();
	private Vector<Integer> uniqueAuth = new Vector<Integer>();
	private Vector<String> unique1stAuthDate = new Vector<String>();
	private Vector<String> unique1stOwner = new Vector<String>();
	
	//Trades (for testing only)
	private Vector<String> oldOwner = new Vector<String>();
	private Vector<String> newOwner = new Vector<String>();
	private Vector<String> tradeDate = new Vector<String>();
	private Vector<Float> tradeLatitude = new Vector<Float>();
	private Vector<Float> tradeLongitude = new Vector<Float>();
	private Vector<byte[]> tradeSignature = new Vector<byte[]>();
	
	//images path
	private String imagesPath=new String("./cardImages/");
	private String previewPath=new String("./cardImages/cardPreviews/");
	
	
	public CardDataToServerDB() {
		
		createCategory();
		createCards();
		createUniqueCards();
		createDefaultTestTrades();
		
	}
	
	
	private void createCategory(){
		Integer count=10;
		//categories
		categoryID.add(count++);
		categories.add("Hero");
		allowSub.add(0);
		
		categoryID.add(count++);
		categories.add("Scourge Hero");
		allowSub.add(0);
		
		categoryID.add(count++);
		categories.add("Ally");
		allowSub.add(0);
		
		categoryID.add(count++);
		categories.add("Armor");
		allowSub.add(0);
		
		categoryID.add(count++);
		categories.add("Weapon");
		allowSub.add(0);
		
		categoryID.add(count++);
		categories.add("Object");
		allowSub.add(0);
		
		categoryID.add(count++);
		categories.add("Ability");
		allowSub.add(0);
		
		categoryID.add(count++);
		categories.add("Quest");
		allowSub.add(0);
		
		//subcategories
		count=1000;
		categoryID.add(count++);
		categories.add("Lich King");
		allowSub.add(1);
		
		categoryID.add(count++);
		categories.add("Mechanical Chicken");
		allowSub.add(1);
		
		categoryID.add(count++);
		categories.add("Death Knight");
		allowSub.add(1);
		
		categoryID.add(count++);
		categories.add("Turtle");
		allowSub.add(1);
		
		categoryID.add(count++);
		categories.add("Druid");
		allowSub.add(1);
		
		categoryID.add(count++);
		categories.add("Rogue");
		allowSub.add(1);
		
		categoryID.add(count++);
		categories.add("Paladin");
		allowSub.add(1);
		
		categoryID.add(count++);
		categories.add("Warrior");
		allowSub.add(1);
		
		categoryID.add(count++);
		categories.add("Mage");
		allowSub.add(1);
		
		categoryID.add(count++);
		categories.add("Mount");
		allowSub.add(1);
		
		categoryID.add(count++);
		categories.add("Shaman");
		allowSub.add(1);
		
		categoryID.add(count++);
		categories.add("Combat Combo");
		allowSub.add(1);
		
		categoryID.add(count++);
		categories.add("Polearm");
		allowSub.add(1);
		
		categoryID.add(count++);
		categories.add("Gorilla");
		allowSub.add(1);
		
		categoryID.add(count++);
		categories.add("Mail");
		allowSub.add(1);
		
		return;
	}

	private void createCards(){
		
		JSONObject jsonCreator;	//uso un oggetto unico, mi server solo la tringa che genera
        byte[] imageTmp;
		byte[] previewTmp;
        //template per inserimento carte
		/*
		cardSN.add("");
		cardName.add("");
		cardStats.add("");
		cardSpecial.add("");
		cardCat.add(categories.indexOf(""));
		cardSubCat.add(subcategories.indexOf(""));
		cardPreview.add(null);
		cardImage.add(null);
		*/
		
		//start card
			cardSN.add("TOMB_99_202_DAEDAK_THE_GRAVEBORNE_WOW");
			cardName.add("Daedak the Graveborne");
			cardSpecial.add("Eternal, Protector");
			cardCat.add(categoryID.get(categories.indexOf("Ally")));
			cardSubCat.add(categoryID.get(categories.indexOf("Death Knight")));
			try {
				imageTmp = fileImageToBytes(imagesPath+"Daedak_the_Graveborne_wow.jpg");
			} catch (IOException e1) {
				imageTmp=null;		e1.printStackTrace();
			}
			try {
				previewTmp = fileImageToBytes(previewPath+"Daedak_the_Graveborne_wow.jpg");
			} catch (IOException e1) {
				previewTmp=null;	e1.printStackTrace();
			}
			cardPreview.add(imageTmp);
			cardImage.add(previewTmp);
			jsonCreator = new JSONObject();
			try {
				jsonCreator.put( "Cost to play", "4");
				jsonCreator.put( "Attack type", "Shadow");
				jsonCreator.put( "Attack points", "4");
				jsonCreator.put( "Health points", "3");
	        } catch (JSONException e) {	e.printStackTrace();}
			cardStats.add(jsonCreator.toString());
		//end card
		
		//start card
			cardSN.add("AZEROTH_283_361_DEATHDEALER_BREASTPLATE_WOW");
			cardName.add("Deathdealer Brestplate");
			cardSpecial.add("Chest (1)");
			cardCat.add(categoryID.get(categories.indexOf("Armor")));
			cardSubCat.add(categoryID.get(categories.indexOf("Mail")));
			try {
				imageTmp = fileImageToBytes(imagesPath+"deathdealer_breastplate_wow.jpg");
			} catch (IOException e1) {
				imageTmp=null;		e1.printStackTrace();
			}
			try {
				previewTmp = fileImageToBytes(previewPath+"deathdealer_breastplate_wow.jpg");
			} catch (IOException e1) {
				previewTmp=null;	e1.printStackTrace();
			}
			cardPreview.add(imageTmp);
			cardImage.add(previewTmp);
			jsonCreator = new JSONObject();
			try {
				jsonCreator.put( "Cost to play", "5");
				jsonCreator.put( "Defense type", "Physical");
				jsonCreator.put( "Defense points", "1");
				jsonCreator.put( "Attack bonus", "+2");
				jsonCreator.put( "Class", "Hunter, Shaman, Warrior, Paladin");
			} catch (JSONException e) {	e.printStackTrace();}
			cardStats.add(jsonCreator.toString());
		//end card
		
		//start card
			cardSN.add("AZEROTH_321_361_FLAME_WRATH_WOW");
			cardName.add("Flame Wrath");
			cardSpecial.add("Melee (1)");
			cardCat.add(categoryID.get(categories.indexOf("Weapon")));
			cardSubCat.add(categoryID.get(categories.indexOf("Polearm")));
			try {
				imageTmp = fileImageToBytes(imagesPath+"flame_wrath_wow.jpg");
			} catch (IOException e1) {
				imageTmp=null;		e1.printStackTrace();
			}
			try {
				previewTmp = fileImageToBytes(previewPath+"flame_wrath_wow.jpg");
			} catch (IOException e1) {
				previewTmp=null;	e1.printStackTrace();
			}
			cardPreview.add(imageTmp);
			cardImage.add(previewTmp);
			jsonCreator = new JSONObject();
			try {
				jsonCreator.put( "Cost to play", "4");
				jsonCreator.put( "Cost to hit", "3");
				jsonCreator.put( "Attack type", "Physical");
				jsonCreator.put( "Attack points", "2");
				jsonCreator.put( "Attack bonus", "+1 fire");
				jsonCreator.put( "Class", "Hunter, Warrior, Paladin");
			} catch (JSONException e) {	e.printStackTrace();}
			cardStats.add(jsonCreator.toString());
		//end card
		
		//start card
			cardSN.add("OUTLAND_LOOT_2_3_GONE_FISHIN_WOW");
			cardName.add("Gone Fishin'");
			cardSpecial.add("Draw 3 cards and skip your next turn");
			cardCat.add(categoryID.get(categories.indexOf("Ability")));
			cardSubCat.add(null);
			try {
				imageTmp = fileImageToBytes(imagesPath+"gone_fishin_wow.jpg");
			} catch (IOException e1) {
				imageTmp=null;		e1.printStackTrace();
			}
			try {
				previewTmp = fileImageToBytes(previewPath+"gone_fishin_wow.jpg");
			} catch (IOException e1) {
				previewTmp=null;	e1.printStackTrace();
			}
			cardPreview.add(imageTmp);
			cardImage.add(previewTmp);
			jsonCreator = new JSONObject();
			try {
				jsonCreator.put( "Cost to play", "3");
			} catch (JSONException e) {	e.printStackTrace();}
			cardStats.add(jsonCreator.toString());
		//end card
				
		//start card
			cardSN.add("AZEROTH_99_361_GOURGE_WOW");
			cardName.add("Gouge");
			cardSpecial.add("Exhaust target hero or ally");
			cardCat.add(categoryID.get(categories.indexOf("Ability")));
			cardSubCat.add(categoryID.get(categories.indexOf("Combat Combo")));
			try {
				imageTmp = fileImageToBytes(imagesPath+"gouge_wow.jpg");
			} catch (IOException e1) {
				imageTmp=null;		e1.printStackTrace();
			}
			try {
				previewTmp = fileImageToBytes(previewPath+"gouge_wow.jpg");
			} catch (IOException e1) {
				previewTmp=null;	e1.printStackTrace();
			}
			cardPreview.add(imageTmp);
			cardImage.add(previewTmp);
			jsonCreator = new JSONObject();
			try {
				jsonCreator.put( "Cost to play", "1");
				jsonCreator.put( "Class", "Rogue");
			} catch (JSONException e) {	e.printStackTrace();}
			cardStats.add(jsonCreator.toString());
		//end card

		//start card
			cardSN.add("AZEROTH_191_361_KING_MAGNI_BRONZEBEARD_WOW");
			cardName.add("King Magni Bronzebeard");
			cardSpecial.add("Every turn put 1 dwarf warrior as ally with 1 ATK and 1 HP");
			cardCat.add(categoryID.get(categories.indexOf("Ally")));
			cardSubCat.add(categoryID.get(categories.indexOf("Warrior")));
			try {
				imageTmp = fileImageToBytes(imagesPath+"king_magni_wow.jpg");
			} catch (IOException e1) {
				imageTmp=null;		e1.printStackTrace();
			}
			try {
				previewTmp = fileImageToBytes(previewPath+"king_magni_wow.jpg");
			} catch (IOException e1) {
				previewTmp=null;	e1.printStackTrace();
			}
			cardPreview.add(imageTmp);
			cardImage.add(previewTmp);
			jsonCreator = new JSONObject();
			try {
				jsonCreator.put( "Cost to play", "9");
				jsonCreator.put( "Race", "Dwarf");
				jsonCreator.put( "Faction", "Alliance");
				jsonCreator.put( "Use", "Unique");
				jsonCreator.put( "Attack type", "Physical");
				jsonCreator.put( "Attack points", "6");
				jsonCreator.put( "Health points", "8");
			} catch (JSONException e) {	e.printStackTrace();}
			cardStats.add(jsonCreator.toString());
		//end card


		//start card
			cardSN.add("DARK_PORTAL_LOOT_2_3_KING_MUKLA_WOW");
			cardName.add("King Mukla");
			cardSpecial.add("Gain control of target ally with cost 3");
			cardCat.add(categoryID.get(categories.indexOf("Ally")));
			cardSubCat.add(categoryID.get(categories.indexOf("Gorilla")));
			try {
				imageTmp = fileImageToBytes(imagesPath+"king_mukla_wow.jpg");
			} catch (IOException e1) {
				imageTmp=null;		e1.printStackTrace();
			}
			try {
				previewTmp = fileImageToBytes(previewPath+"king_mukla_wow.jpg");
			} catch (IOException e1) {
				previewTmp=null;	e1.printStackTrace();
			}
			cardPreview.add(imageTmp);
			cardImage.add(previewTmp);
			jsonCreator = new JSONObject();
			try {
				jsonCreator.put( "Cost to play", "7");
				jsonCreator.put( "Race", "Beast");
				jsonCreator.put( "Use", "Unique");
				jsonCreator.put( "Attack type", "Physical");
				jsonCreator.put( "Attack points", "6");
				jsonCreator.put( "Health points", "5");
			} catch (JSONException e) {	e.printStackTrace();}
			cardStats.add(jsonCreator.toString());
		//end card

			
	//start card
		cardSN.add("LEGION_LOOT_3_3_KITING_WOW");
		cardName.add("Kiting");
		cardSpecial.add("Target cannot deal combat damage");
		cardCat.add(categoryID.get(categories.indexOf("Ability")));
		cardSubCat.add(null);
		try {
			imageTmp = fileImageToBytes(imagesPath+"kiting_wow.jpg");
		} catch (IOException e1) {
			imageTmp=null;		e1.printStackTrace();
		}
		try {
			previewTmp = fileImageToBytes(previewPath+"kiting_wow.jpg");
		} catch (IOException e1) {
			previewTmp=null;	e1.printStackTrace();
		}
		cardPreview.add(imageTmp);
		cardImage.add(previewTmp);
		jsonCreator = new JSONObject();
		try {
			jsonCreator.put( "Cost to play", "4");
		} catch (JSONException e) {	e.printStackTrace();}
		cardStats.add(jsonCreator.toString());
	//end card


		//start card
			cardSN.add("CITADEL_RAID_1_93_THE_LICH_KING_WOW");
			cardName.add("The Lich King");
			cardSpecial.add("You can use Frostmourne or Helm of Domination");
			cardCat.add(categoryID.get(categories.indexOf("Scourge Hero")));
			cardSubCat.add(categoryID.get(categories.indexOf("Lich King")));
			try {
				imageTmp = fileImageToBytes(imagesPath+"lichking_wow.jpg");
			} catch (IOException e1) {
				imageTmp=null;		e1.printStackTrace();
			}
			try {
				previewTmp = fileImageToBytes(previewPath+"lichking_wow.jpg");
			} catch (IOException e1) {
				previewTmp=null;	e1.printStackTrace();
			}
			cardPreview.add(imageTmp);
			cardImage.add(previewTmp);
			jsonCreator = new JSONObject();
			try {
				jsonCreator.put( "Cost to play", "10");
				jsonCreator.put( "Race", "Lich");
				jsonCreator.put( "Use", "Unique");
				jsonCreator.put( "Health points", "60");
			} catch (JSONException e) {	e.printStackTrace();}
			cardStats.add(jsonCreator.toString());
		//end card

		//start card
			cardSN.add("AZEROTH_203_361_LT_COMMANDER_DUDEFELLA_WOW");
			cardName.add("Lt. Commander Dudefella");
			cardSpecial.add("deal 1 frost damage to target");
			cardCat.add(categoryID.get(categories.indexOf("Ally")));
			cardSubCat.add(categoryID.get(categories.indexOf("Mage")));
			try {
				imageTmp = fileImageToBytes(imagesPath+"lt_commander_dudefella_wow.jpg");
			} catch (IOException e1) {
				imageTmp=null;		e1.printStackTrace();
			}
			try {
				previewTmp = fileImageToBytes(previewPath+"lt_commander_dudefella_wow.jpg");
			} catch (IOException e1) {
				previewTmp=null;	e1.printStackTrace();
			}
			cardPreview.add(imageTmp);
			cardImage.add(previewTmp);
			jsonCreator = new JSONObject();
			try {
				jsonCreator.put( "Cost to play", "3");
				jsonCreator.put( "Race", "Human");
				jsonCreator.put( "Faction", "Alliance");
				jsonCreator.put( "Attack points", "3");
				jsonCreator.put( "Attack type", "Frost");
				jsonCreator.put( "Health points", "3");
			} catch (JSONException e) {	e.printStackTrace();}
			cardStats.add(jsonCreator.toString());
		//end card
			
			//start card
			cardSN.add("LEGION_LOOT_1_3_PAPER_AIRPLANE_WOW");
			cardName.add("Paper Airplane");
			cardSpecial.add("Put an airplane on top of target");
			cardCat.add(categoryID.get(categories.indexOf("Ability")));
			cardSubCat.add(null);
			try {
				imageTmp = fileImageToBytes(imagesPath+"paper_airplane_wow.jpg");
			} catch (IOException e1) {
				imageTmp=null;		e1.printStackTrace();
			}
			try {
				previewTmp = fileImageToBytes(previewPath+"paper_airplane_wow.jpg");
			} catch (IOException e1) {
				previewTmp=null;	e1.printStackTrace();
			}
			cardPreview.add(imageTmp);
			cardImage.add(previewTmp);
			jsonCreator = new JSONObject();
			try {
				jsonCreator.put( "Cost to play", "1");
			} catch (JSONException e) {	e.printStackTrace();}
			cardStats.add(jsonCreator.toString());
		//end card

			//start card
			cardSN.add("BETRAYER_LOOT_1_3_PET_BISCUIT_WOW");
			cardName.add("Papa Hummel's old-fashioned Pet Biscuit");
			cardSpecial.add("Put X growth counter on the pet");
			cardCat.add(categoryID.get(categories.indexOf("Ability")));
			cardSubCat.add(null);
			try {
				imageTmp = fileImageToBytes(imagesPath+"Pet_Biscuit_Loot_wow.jpg");
			} catch (IOException e1) {
				imageTmp=null;		e1.printStackTrace();
			}
			try {
				previewTmp = fileImageToBytes(previewPath+"Pet_Biscuit_Loot_wow.jpg");
			} catch (IOException e1) {
				previewTmp=null;	e1.printStackTrace();
			}
			cardPreview.add(imageTmp);
			cardImage.add(previewTmp);
			jsonCreator = new JSONObject();
			try {
				jsonCreator.put( "Cost to play", "X");
			} catch (JSONException e) {	e.printStackTrace();}
			cardStats.add(jsonCreator.toString());
		//end card


			//start card
				cardSN.add("BETRAYER_16_264_REESA_THE_LEPER_QUEEN_WOW");
				cardName.add("Reesa the Leper Queen");
				cardSpecial.add("destroy 1 target equipment");
				cardCat.add(categoryID.get(categories.indexOf("Hero")));
				cardSubCat.add(categoryID.get(categories.indexOf("Rogue")));
				try {
					imageTmp = fileImageToBytes(imagesPath+"reesa_wow.jpg");
				} catch (IOException e1) {
					imageTmp=null;		e1.printStackTrace();
				}
				try {
					previewTmp = fileImageToBytes(previewPath+"reesa_wow.jpg");
				} catch (IOException e1) {
					previewTmp=null;	e1.printStackTrace();
				}
				cardPreview.add(imageTmp);
				cardImage.add(previewTmp);
				jsonCreator = new JSONObject();
				try {
					jsonCreator.put( "Cost to play", "3");
					jsonCreator.put( "Race", "Gnome");
					jsonCreator.put( "Class", "Rogue");
					jsonCreator.put( "Faction", "Alliance");
					jsonCreator.put( "Health points", "27");
				} catch (JSONException e) {	e.printStackTrace();}
				cardStats.add(jsonCreator.toString());
			//end card

				//start card
				cardSN.add("LEGION_LOOT_2_3_ROBOTIC_HOMING_CHICKEN_WOW");
				cardName.add("Robotic Homing Chicken");
				cardSpecial.add("Elusive, throw the dice");
				cardCat.add(categoryID.get(categories.indexOf("Ally")));
				cardSubCat.add(categoryID.get(categories.indexOf("Mechanical Chicken")));
				try {
					imageTmp = fileImageToBytes(imagesPath+"robotic_homing_chicken_wow.jpg");
				} catch (IOException e1) {
					imageTmp=null;		e1.printStackTrace();
				}
				try {
					previewTmp = fileImageToBytes(previewPath+"robotic_homing_chicken_wow.jpg");
				} catch (IOException e1) {
					previewTmp=null;	e1.printStackTrace();
				}
				cardPreview.add(imageTmp);
				cardImage.add(previewTmp);
				jsonCreator = new JSONObject();
				try {
					jsonCreator.put( "Cost to play", "1");
					jsonCreator.put( "Race", "Mecha");
					jsonCreator.put( "Health points", "1");
					jsonCreator.put( "Attack type", "Physical");
					jsonCreator.put( "Attack points", "0");
				} catch (JSONException e) {	e.printStackTrace();}
				cardStats.add(jsonCreator.toString());
			//end card
				

			//start card
				cardSN.add("KNIGHT_2_27_SAERYN_DUSKSORROW_WOW");
				cardName.add("Saeryn Dusksorrow");
				cardSpecial.add("put target weapon card from graveyard into hand");
				cardCat.add(categoryID.get(categories.indexOf("Hero")));
				cardSubCat.add(categoryID.get(categories.indexOf("Death Knight")));
				try {
					imageTmp = fileImageToBytes(imagesPath+"saeryn_dusksorrow_wow.jpg");
				} catch (IOException e1) {
					imageTmp=null;		e1.printStackTrace();
				}
				try {
					previewTmp = fileImageToBytes(previewPath+"saeryn_dusksorrow_wow.jpg");
				} catch (IOException e1) {
					previewTmp=null;	e1.printStackTrace();
				}
				cardPreview.add(imageTmp);
				cardImage.add(previewTmp);
				jsonCreator = new JSONObject();
				try {
					jsonCreator.put( "Cost to play", "3");
					jsonCreator.put( "Race", "Blood Elf");
					jsonCreator.put( "Faction", "Horde");
					jsonCreator.put( "Health points", "29");
				} catch (JSONException e) {	e.printStackTrace();}
				cardStats.add(jsonCreator.toString());
			//end card

			//start card
				cardSN.add("AZEROTH_LOOT_3_3_SALTWATER_SNAPJAW_WOW");
				cardName.add("Saltwater Snapjaw");
				cardSpecial.add("prevent all damage dealt to the ally");
				cardCat.add(categoryID.get(categories.indexOf("Ally")));
				cardSubCat.add(categoryID.get(categories.indexOf("Turtle")));
				try {
					imageTmp = fileImageToBytes(imagesPath+"saltwater_snapjaw_wow.jpg");
				} catch (IOException e1) {
					imageTmp=null;		e1.printStackTrace();
				}
				try {
					previewTmp = fileImageToBytes(previewPath+"saltwater_snapjaw_wow.jpg");
				} catch (IOException e1) {
					previewTmp=null;	e1.printStackTrace();
				}
				cardPreview.add(imageTmp);
				cardImage.add(previewTmp);
				jsonCreator = new JSONObject();
				try {
					jsonCreator.put( "Cost to play", "3");
					jsonCreator.put( "Attack type", "Physical");
					jsonCreator.put( "Attack points", "2");
					jsonCreator.put( "Health points", "3");
				} catch (JSONException e) {	e.printStackTrace();}
				cardStats.add(jsonCreator.toString());
			//end card

			//start card
				cardSN.add("DRUMS_LOOT_1_3_SLASHDANCE_WOW");
				cardName.add("Slashdance");
				cardSpecial.add("Make a hero dance");
				cardCat.add(categoryID.get(categories.indexOf("Ability")));
				cardSubCat.add(null);
				try {
					imageTmp = fileImageToBytes(imagesPath+"slashdance_wow.jpg");
				} catch (IOException e1) {
					imageTmp=null;		e1.printStackTrace();
				}
				try {
					previewTmp = fileImageToBytes(previewPath+"slashdance_wow.jpg");
				} catch (IOException e1) {
					previewTmp=null;	e1.printStackTrace();
				}
				cardPreview.add(imageTmp);
				cardImage.add(previewTmp);
				jsonCreator = new JSONObject();
				try {
					jsonCreator.put( "Cost to play", "1");
				} catch (JSONException e) {	e.printStackTrace();}
				cardStats.add(jsonCreator.toString());
			//end card
				
				//start card
				cardSN.add("OUTLAND_LOOT_3_3_SPECTRAL_TIGER_WOW");
				cardName.add("Spectral tiger");
				cardSpecial.add("");
				cardCat.add(categoryID.get(categories.indexOf("Ability")));
				cardSubCat.add(categoryID.get(categories.indexOf("Mount")));
				try {
					imageTmp = fileImageToBytes(imagesPath+"spectral_tiget_wow.jpg");
				} catch (IOException e1) {
					imageTmp=null;		e1.printStackTrace();
				}
				try {
					previewTmp = fileImageToBytes(previewPath+"spectral_tiget_wow.jpg");
				} catch (IOException e1) {
					previewTmp=null;	e1.printStackTrace();
				}
				cardPreview.add(imageTmp);
				cardImage.add(previewTmp);
				jsonCreator = new JSONObject();
				try {
					jsonCreator.put( "Cost to play", "4");
					jsonCreator.put( "Attack type", "Physical");
					jsonCreator.put( "Attack points", "4");
					jsonCreator.put( "Health points", "3");
					jsonCreator.put( "Alliance", "Elusive allies +1 HP");
					jsonCreator.put( "Horde", "Ferocity allies +1 ATK");
				} catch (JSONException e) {	e.printStackTrace();}
				cardStats.add(jsonCreator.toString());
			//end card

			//start card
				cardSN.add("BETRAYER_156_264_STEFEN_COLBEAR_WOW");
				cardName.add("Stefen Colbear");
				cardSpecial.add("Truthiness");
				cardCat.add(categoryID.get(categories.indexOf("Ally")));
				cardSubCat.add(categoryID.get(categories.indexOf("Paladin")));
				try {
					imageTmp = fileImageToBytes(imagesPath+"stephen_colbert_wow.jpg");
				} catch (IOException e1) {
					imageTmp=null;		e1.printStackTrace();
				}
				try {
					previewTmp = fileImageToBytes(previewPath+"stephen_colbert_wow.jpg");
				} catch (IOException e1) {
					previewTmp=null;	e1.printStackTrace();
				}
				cardPreview.add(imageTmp);
				cardImage.add(previewTmp);
				jsonCreator = new JSONObject();
				try {
					jsonCreator.put( "Cost to play", "6");
					jsonCreator.put( "Race", "Human");
					jsonCreator.put( "Class", "Paladin");
					jsonCreator.put( "Faction", "Alliance");
					jsonCreator.put( "Attack points", "6");
					jsonCreator.put( "Attack type", "Physical");
					jsonCreator.put( "Health points", "6");
				} catch (JSONException e) {	e.printStackTrace();}
				cardStats.add(jsonCreator.toString());
			//end card

				//start card
				cardSN.add("AZEROTH_15_361_TA_ZO_WOW");
				cardName.add("Ta'zo");
				cardSpecial.add("Fire damage to target");
				cardCat.add(categoryID.get(categories.indexOf("Hero")));
				cardSubCat.add(categoryID.get(categories.indexOf("Mage")));
				try {
					imageTmp = fileImageToBytes(imagesPath+"ta_zo_wow.jpg");
				} catch (IOException e1) {
					imageTmp=null;		e1.printStackTrace();
				}
				try {
					previewTmp = fileImageToBytes(previewPath+"ta_zo_wow.jpg");
				} catch (IOException e1) {
					previewTmp=null;	e1.printStackTrace();
				}
				cardPreview.add(imageTmp);
				cardImage.add(previewTmp);
				jsonCreator = new JSONObject();
				try {
					jsonCreator.put( "Cost to play", "3");
					jsonCreator.put( "Race", "Troll");
					jsonCreator.put( "Class", "Mage");
					jsonCreator.put( "Faction", "Horde");
					jsonCreator.put( "Attack points", "3");
					jsonCreator.put( "Attack type", "Fire");
					jsonCreator.put( "Health points", "25");
				} catch (JSONException e) {	e.printStackTrace();}
				cardStats.add(jsonCreator.toString());
			//end card
				
				//start card
				cardSN.add("AZEROTH_267_361_WARCHIEF_THRALL_WOW");
				cardName.add("Warchief Thrall");
				cardSpecial.add("bonus to other Horde allies");
				cardCat.add(categoryID.get(categories.indexOf("Ally")));
				cardSubCat.add(categoryID.get(categories.indexOf("Shaman")));
				try {
					imageTmp = fileImageToBytes(imagesPath+"thrall_wow.jpg");
				} catch (IOException e1) {
					imageTmp=null;		e1.printStackTrace();
				}
				try {
					previewTmp = fileImageToBytes(previewPath+"thrall_wow.jpg");
				} catch (IOException e1) {
					previewTmp=null;	e1.printStackTrace();
				}
				cardPreview.add(imageTmp);
				cardImage.add(previewTmp);
				jsonCreator = new JSONObject();
				try {
					jsonCreator.put( "Cost to play", "9");
					jsonCreator.put( "Race", "Orc");
					jsonCreator.put( "Class", "Shaman");
					jsonCreator.put( "Use", "Unique");
					jsonCreator.put( "Faction", "Horde");
					jsonCreator.put( "Attack points", "7");
					jsonCreator.put( "Attack type", "Physical");
					jsonCreator.put( "Health points", "8");
					jsonCreator.put( "Attack bonus", "+3");
					jsonCreator.put( "Health bonus", "+3");
				} catch (JSONException e) {	e.printStackTrace();}
				cardStats.add(jsonCreator.toString());
			//end card
				

				//start card
				cardSN.add("AZEROTH_LOOT_2_3_THUNDERHEAD_HIPPOGRYPH_WOW");
				cardName.add("Thunderhead Hippogryph");
				cardSpecial.add("Mount");
				cardCat.add(categoryID.get(categories.indexOf("Ally")));
				cardSubCat.add(categoryID.get(categories.indexOf("Mount")));
				try {
					imageTmp = fileImageToBytes(imagesPath+"thunderhead_hippogryph_wow.jpg");
				} catch (IOException e1) {
					imageTmp=null;		e1.printStackTrace();
				}
				try {
					previewTmp = fileImageToBytes(previewPath+"thunderhead_hippogryph_wow.jpg");
				} catch (IOException e1) {
					previewTmp=null;	e1.printStackTrace();
				}
				cardPreview.add(imageTmp);
				cardImage.add(previewTmp);
				jsonCreator = new JSONObject();
				try {
					jsonCreator.put( "Cost to play", "4");
					jsonCreator.put( "Attack type", "Physical");
					jsonCreator.put( "Attack points", "3");
					jsonCreator.put( "Health points", "3");
					jsonCreator.put( "Alliance", "has Elusive");
					jsonCreator.put( "Horde", "has Ferocity");
				} catch (JSONException e) {	e.printStackTrace();}
				cardStats.add(jsonCreator.toString());
			//end card

				//start card
				cardSN.add("AZEROTH_360_361_YOUR_FORTUNE_AWAITS_YOU_WOW");
				cardName.add("Your Fortune Awaits You");
				cardSpecial.add("Reward: draw a card");
				cardCat.add(categoryID.get(categories.indexOf("Quest")));
				cardSubCat.add(null);
				try {
					imageTmp = fileImageToBytes(imagesPath+"your_fortune_wow.jpg");
				} catch (IOException e1) {
					imageTmp=null;		e1.printStackTrace();
				}
				try {
					previewTmp = fileImageToBytes(previewPath+"your_fortune_wow.jpg");
				} catch (IOException e1) {
					previewTmp=null;	e1.printStackTrace();
				}
				cardPreview.add(imageTmp);
				cardImage.add(previewTmp);
				jsonCreator = new JSONObject();
				try {
					jsonCreator.put( "Cost to play", "3");
				} catch (JSONException e) {	e.printStackTrace();}
				cardStats.add(jsonCreator.toString());
			//end card

				
		return;
}
	
	private void createUniqueCards(){
		int cardsNumber= cardSN.size();
		
		//per ogni carta creo 5 carte uniche
		for (int i = 0; i < cardsNumber; i++) {
			for(int j=1; j<=5;j++){
				uniqueSN.add(cardSN.get(i));
				uniqueID.add("UNIQUE_CARD_ID_"+j);
				uniqueActOwn.add(null); //nessuna carta è stata autenticata!!!!!
				uniqueAuth.add(0);
				unique1stAuthDate.add(null);
				unique1stOwner.add(null);
			}
		}
		
		return;
	}
	
	private void createDefaultTestTrades(){
		/*Date d = new Date(Long.parseLong("1356268397598"));
		float baseLong = (float) 10.807251;
		float baseLat = (float) 44.995883;
		
		for (int i = 0; i < 5; i++) {
			oldOwner.add("test00" + (i+1) + "@gmail.com");
			newOwner.add("test00" + (i+2)  + "@gmail.com");
			
			tradeDate.add(d.toString());
			d = new Date((long)(d.getTime() + 6.048e8 + 86400000*(int)(Math.random() * ((5 - 1) + 1))));
			
			tradeLongitude.add((float) (Math.random() + baseLong));
			tradeLatitude.add((float) (Math.random() + baseLat));
			
			tradeSignature.add(SecurityUtilities.signData(("AZEROTH_LOOT_2_3_THUNDERHEAD_HIPPOGRYPH_WOW" + "UNIQUE_CARD_ID_2" + oldOwner.get(i) + newOwner.get(i)).getBytes(), SecurityManager.getPrivateKey()));
		}*/
	}
	
	public void putCardsIntoServerDB(SqlJetDb db){
		try {
			ISqlJetTable categoriesTable = db.getTable(TABLE_NAME_CATEGORIES);
			for (int i = 0; i < categoryID.size(); i++) {
				categoriesTable.insert(categoryID.get(i), categories.get(i),allowSub.get(i));
			}
			
			ISqlJetTable cardsTable = db.getTable(TABLE_NAME_CARDS);
			for (int i = 0; i < cardSN.size(); i++) {
				cardsTable.insert(cardSN.get(i), cardName.get(i),cardStats.get(i),cardSpecial.get(i),
									cardCat.get(i),cardSubCat.get(i),cardImage.get(i),cardPreview.get(i));
			}
			
			ISqlJetTable uniquecardsTable = db.getTable(TABLE_NAME_UNIQUECARDS);
			for (int i = 0; i < uniqueID.size(); i++) {
				uniquecardsTable.insert(uniqueID.get(i),uniqueSN.get(i),uniqueActOwn.get(i),uniqueAuth.get(i),
								unique1stAuthDate.get(i),unique1stOwner.get(i));
			}
			
			ISqlJetTable tradesTable = db.getTable(TABLE_NAME_TRADES);
			for (int i = 0; i < 5; i++) {
				tradesTable.insert("UNIQUE_CARD_ID_2", "AZEROTH_LOOT_2_3_THUNDERHEAD_HIPPOGRYPH_WOW",
						newOwner.get(i), oldOwner.get(i), tradeDate.get(i), tradeLatitude.get(i), 
						tradeLongitude.get(i), tradeSignature.get(i));
			}
			
			db.commit();
		} catch (SqlJetException e) {e.printStackTrace();}
		
		
		return;
		
	}
	
	private byte[] fileImageToBytes (String imageName) throws IOException {
		File fnew=new File(imageName);
		BufferedImage originalImage=ImageIO.read(fnew);
		ByteArrayOutputStream baos=new ByteArrayOutputStream();
		ImageIO.write(originalImage, "jpg", baos );
		return baos.toByteArray();	
	}
}
